Skip to content

Include quoted Feishu text in agent body#497

Open
zhulijin1991 wants to merge 1 commit into
larksuite:mainfrom
zhulijin1991:codex/lark-quoted-body-for-agent
Open

Include quoted Feishu text in agent body#497
zhulijin1991 wants to merge 1 commit into
larksuite:mainfrom
zhulijin1991:codex/lark-quoted-body-for-agent

Conversation

@zhulijin1991
Copy link
Copy Markdown
Contributor

@zhulijin1991 zhulijin1991 commented May 11, 2026

Summary

  • Drop the BodyForAgent quote injection so quoted text is not duplicated with the SDK ReplyToBody context block.
  • Carry structured quoted-message mentions from lookup/enrichment into dispatch.
  • Detect quoted-message bot mentions by comparing mention open_id values with the current bot open_id, instead of matching display-name strings.

Trigger scenario

A user replies to a Feishu message that mentioned the bot, while the new reply text itself does not include the bot mention. The intent is to count that quoted structured mention as bot-directed without duplicating quoted text in BodyForAgent.

Validation

  • pnpm vitest run tests/dispatch-builders-body-for-agent.test.ts tests/dispatch-tool-use-init.test.ts tests/empty-message-guard.test.ts
  • pnpm typecheck
  • pnpm lint
  • git diff --check

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 11, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Collaborator

@evandance evandance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — the underlying questions are real (does the agent see quote context, and if the user replied to a message that mentioned the bot, should that count). But I can't recommend merging this in its current form. Two things, both verifiable from the code.

First, the quote injection into BodyForAgent duplicates context the agent already gets. dispatch.ts:468 already passes quotedContent as replyToBody to buildInboundPayload, and the SDK's buildInboundUserContextPrefix (reply-BwK-bN2w.js:1617-1626) unconditionally renders it as a "Replied message (untrusted, for context):" JSON block in the user-context prefix. After this PR the same text shows up twice in the prompt — once at the top of BodyForAgent as [Replying to: "..."], once as the SDK metadata block. The PR description says "SDK fallback paths prefer BodyForAgent over Body/ReplyToBody" — that's actually the BodyForAgent ?? CommandBody ?? RawBody ?? Body fallback chain, not a choice between BodyForAgent and ReplyToBody. The two render independently in the prompt, so this is duplication, not redundancy.

Second, quotedContentMentionsCurrentBot is broken on inspection. The candidate set is [account.config?.name, account.accountId, route.agentId] — the latter two are openclaw.json configuration keys (e.g. "default", "main"), they never appear in Feishu message text, so only account.config?.name can ever match. The three string patterns assume conflicting input formats: @${name} for plain-text-rendered mentions, >${name}< and ${name}</at> for the raw <at user_id="ou_xxx">Display</at> structure. quotedContent is one shape or the other, never both — so two of the three patterns are always misses. The function also has no tests; the added test file only covers buildBodyForAgent's quote injection.

For this to land, two changes:

  1. Drop the BodyForAgent quote injection. If there's a real case where ReplyToBody isn't reaching the agent today, paste the trace into the PR description and we'll diagnose — but the fix shouldn't be a parallel injection that duplicates content for the normal case.
  2. Rewrite mention detection to use structured data rather than string matching on rendered text. The Feishu API response from /open-apis/im/v1/messages/mget already returns a mentions array — the codebase already parses it via buildConvertContextFromItem in message-lookup.ts and uses the same pattern in parse.ts for inbound messages — and the bot's own open_id is available via LarkClient. Comparing IDs sidesteps display-name fragility entirely. Add a test covering both "bot mentioned in quote" and "bot not mentioned" cases.

@evandance evandance added feature request New feature or request messaging src/messaging/ + src/card/ — message rendering, cards, streaming changes requested Need do changes labels May 12, 2026
@zhulijin1991
Copy link
Copy Markdown
Contributor Author

Thanks, this is helpful. I agree the BodyForAgent quote injection duplicates ReplyToBody for the normal path, so I will drop that part instead of parallel-injecting the same context.

For mention detection, I will rework it around structured mention IDs from the Feishu message payload / mget path rather than display-name string matching, and add positive + negative tests for “bot mentioned in quoted message”. The scenario behind the PR was a reply where the quoted message mentioned the bot but the new message text did not; I will capture that trace in the PR description so we can verify the right layer before reworking the patch.

@zhulijin1991 zhulijin1991 force-pushed the codex/lark-quoted-body-for-agent branch from ee21d82 to 679764c Compare May 15, 2026 14:18
@zhulijin1991
Copy link
Copy Markdown
Contributor Author

Updated in 679764c.

Changes:

  • Rebased onto current upstream/main, resolving the conflict.
  • Dropped the BodyForAgent quote injection entirely; quoted text remains in ReplyToBody.
  • Kept mention detection based on structured quoted-message mention open_ids instead of display-text matching.
  • Added positive and negative tests for quoted-message bot mention detection.

Validation:

  • pnpm test tests/dispatch-tool-use-init.test.ts tests/empty-message-guard.test.ts
  • pnpm typecheck

Ready for re-review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes requested Need do changes feature request New feature or request messaging src/messaging/ + src/card/ — message rendering, cards, streaming

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants